package org.ioc.com.spring_trans.controller;

import lombok.SneakyThrows;
import org.ioc.com.spring_trans.mapper.UserInfoMapper;
import org.ioc.com.spring_trans.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    /*
    程序正常执行
    事务自动提交
     */
    /*@Transactional
    @RequestMapping("/register")
    public Integer register(String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        return  result;
    }
    *//*
    抛出异常, @Transactional
    事务自动回滚
     *//*
    @Transactional
    @RequestMapping("/r1")
    public Integer r1(String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        int a=10/0;
        return  result;

    }
    *//*
    利用try_catch对异常进行了捕获
     @Transactional感知不到异常
     还是自动 提交事务
     *//*
    @Transactional
    @RequestMapping("/r2")
    public Integer r2(String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        try {
            int a=10/0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;

    }
    *//*
        捕获异常以后,内部处理了 @Transactional感知不到,如果还想回滚可以手动设置
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
     *//*
    @Transactional
    @RequestMapping("/r3")
    public Integer r3(String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        try {
            int a=10/0;
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return  result;

    }
    *//*
    这里抛出Exception异常,但是事务还是会提交
     *//*

    @Transactional
    @RequestMapping("/r4")
    public Integer r4(String userName,String password) throws Exception {
        Integer result=userInfoService.insert(userName,password);
        if(true){
            throw new Exception();
        }
        return  result;

    }

    *//*
    抛出RuntimeException时异常事务会回滚
    RuntimeException和Error会自动触发回滚
    其他的异常不会.还是会正常提交
     *//*
    @Transactional
    @RequestMapping("/r5")
    public Integer r5(String userName,String password) throws Exception {
        Integer result=userInfoService.insert(userName,password);
        if(true){
            throw new RuntimeException();
        }
        return  result;

    }
    *//*
    @Transactional括号中的是声明所有异常都必须要回滚
     *//*
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r6")
    public Integer r6(String userName,String password) throws Exception {
        Integer result=userInfoService.insert(userName,password);
        if(true){
            throw new Exception();
        }
        return  result;

    }*/




    /*
    程序正常执行,自动提交事务
     */
    @Transactional
    @RequestMapping("/register")
    public Integer register(String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        return  result;
    }
    /*
    程序存在异常,自动回滚
     */
    @Transactional
    @RequestMapping("/r1")
    public Integer r1(String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        int a=10/0;
        return  result;
    }
    /*
    用try-catch捕获异常后
    可以自动提交事务

    try-catch捕获后,@Transactional感知不到有异常
     */
    @Transactional
    @RequestMapping("/r3")
    public Integer r3 (String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        try {
            int a=10/0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }
    /*
    重新抛出异常或者手动抛出异常依旧会回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public Integer r4 (String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        try {
            int a=10/0;
        }catch (Exception e){
            //抛出异常
            throw e;
        }
        return  result;
    }
    @Transactional
    @RequestMapping("/r5")
    public Integer r5 (String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        try {
            int a=10/0;
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return  result;
    }
    /*
    事务对于异常的处理
    //Exception不会回滚
     */
    @Transactional
    @SneakyThrows//这个注解也是通过try-catch捕获的异常
    @RequestMapping("/r6")
    public Integer r6 (String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        if (true){
            throw new Exception();
        }
        return  result;
    }
    /*
   事务对于异常的处理
   //异常中RuntimeException和Error会自动回滚,其他的不会
    */
    @Transactional

    @RequestMapping("/r7")
    public Integer r7 (String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        if (true){
            throw new RuntimeException();
        }
        return  result;
    }


    /*
    注解中指定所有异常都要回滚
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r8")
    @SneakyThrows
    public Integer r8 (String userName,String password){
        Integer result=userInfoService.insert(userName,password);
        if (true){
            throw new Exception();
        }
        return  result;
    }

}
